package com.yanze.schoolinformation.view.information.gank;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.example.xrecyclerview.XRecyclerView;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.GankAdapter;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.databinding.FragmentBigAndroidBinding;
import com.yanze.schoolinformation.model.bean.GankDataBean;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.viewModule.gank.BigAndroidNavigator;
import com.yanze.schoolinformation.viewModule.gank.BigAndroidViewModel;

/**
 * Created by MrZheng on 2018/4/27.
 */

public class BigAndroidFragment extends BaseFragment<FragmentBigAndroidBinding> implements BigAndroidNavigator{

    private boolean mIsPrepared;
    private boolean mIsFirst = true;
    private GankAdapter mGankAdapter;
    private BigAndroidViewModel viewModel;

    @Override
    public int setContent() {
        return R.layout.fragment_big_android;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = new BigAndroidViewModel(this);
        viewModel.setNavigator(this);
        initRecyclerView();
        //准备就绪
        mIsPrepared = true;

        loadData();
    }

    private void initRecyclerView() {
        mGankAdapter = new GankAdapter(getContext());
        bindingView.xrvBigAndroid.setLayoutManager(new LinearLayoutManager(getActivity()));
        bindingView.xrvBigAndroid.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.setPage(1);
                viewModel.loadAndroidData();
            }

            @Override
            public void onLoadMore() {
                int page = viewModel.getPage();
                page++;
                viewModel.setPage(page);
                viewModel.loadAndroidData();
            }
        });
        bindingView.xrvBigAndroid.setAdapter(mGankAdapter);
    }

    private void setAdapter(GankDataBean gankDataBean) {
        mGankAdapter.clear();
        mGankAdapter.addAll(gankDataBean.getResults());
        mGankAdapter.notifyDataSetChanged();
        bindingView.xrvBigAndroid.refreshComplete();

        mIsFirst = false;
    }

    @Override
    protected void loadData() {
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        viewModel.loadAndroidData();
    }

    @Override
    public void showLoadSuccessView() {
        showContentView();
    }

    @Override
    public void showAdapterView(GankDataBean gankIoDataBean) {
        setAdapter(gankIoDataBean);
    }

    @Override
    public void refreshAdapter(GankDataBean gankIoDataBean) {
        bindingView.xrvBigAndroid.refreshComplete();
        mGankAdapter.addAll(gankIoDataBean.getResults());
        mGankAdapter.notifyDataSetChanged();
    }

    @Override
    public void showListNoMoreLoading() {
        bindingView.xrvBigAndroid.noMoreLoading();
    }

    @Override
    public void showLoadFailedView() {
        bindingView.xrvBigAndroid.refreshComplete();
        // 注意：这里不能写成 mPage == 1，否则会一直显示错误页面
        if (mGankAdapter.getItemCount() == 0) {
            showError();
        }
    }

    @Override
    protected void onRefresh() {
        viewModel.loadAndroidData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.onDestroy();
    }
}
